package com.example.study.controller;

import com.example.study.entity.Teacher;
import com.example.study.service.ITeacherService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 功能描述：
 *
 * @Author: liqiancheng
 * @Email: liqiancheng@centfor.com
 * @Date: 2020/6/4 16:05
 **/
@RestController
@RequestMapping("/teacher")
public class TeacherController {

    @Resource
    private ITeacherService teacherService;

    @PostMapping("/save")
    public String saveOrUpdate(@RequestBody Teacher teacher) throws JsonProcessingException {
        Teacher newTeacher = teacherService.save(teacher);
        return new ObjectMapper().writeValueAsString(newTeacher);
    }

    @GetMapping("/delete")
    public String delete(@RequestParam int id) throws JsonProcessingException {
        teacherService.delete(id);
        return new ObjectMapper().writeValueAsString("删除成功!");
    }

    @GetMapping("/listAll")
    public String listAll() throws JsonProcessingException {
        return new ObjectMapper().writeValueAsString(teacherService.List());
    }


}
